package com.thread.testThreadLocal;

import java.util.concurrent.atomic.AtomicInteger;

/**
 * @Description: MainTest生成唯一码
 * @author: conceit
 * @Date: 2018/8/28 16:18
 */
public class UniqueThreadIdGenerator {


    /**
     * @Description: 声明唯一id静态变量
     * @author: conceit
     * @Date: 2018/8/28 16:39
     */
    public static AtomicInteger uniqueId = new AtomicInteger(0);

    /**
     * @Description: getCurrentThreadUniqueId
     * @author: conceit
     * @return: int
     * @Date: 2018/8/28 16:43
     */
    public static int getCurrentThreadUniqueId() {
        return uniqueNum.get();
    }

    public static void setUniqueId(int id) {
        uniqueNum.set(id);
    }

    /**
     * @Description: 声明ThreadLocal
     * @author: conceit
     * @Date: 2018/8/28 16:48
     */
    public static ThreadLocal<Integer> uniqueNum = new ThreadLocal<Integer>() {

        @Override
        protected Integer initialValue() {
            return uniqueId.getAndIncrement();
        }

    };
}
